<?php
/**
 * Larfree Api类
 * @author xiao
 */
namespace App\Http\Controllers\Api\Express;
use App\Events\Course\AttendCourse;

use App\Models\User\UserCollect;

use Illuminate\Http\Request;
use Larfree\Controllers\ApisController as Controller;
use App\Models\Express\ExpressCourse;
class CourseController extends Controller
{
    public function __construct(ExpressCourse $model)
    {
        $this->model = $model;
        parent::__construct();
    }

    public function  index(Request $request)
    {
        $recommend =$request->recommend;

        $this->model=$this->model->select(['id','title','user_count','time_count','brief_info','conten_url','created_at','level1','level2'])
            ->link(['level1', 'level2'])
            ->where(function ($query) use($recommend ){
             if(isset($recommend) && $recommend==1){
                $query->where('recommend',1)->orderBy('id','desc')->limit(4);  //取出首页推荐课程，最多四个
             }
             })
            ->where(function ($query) use($recommend ){
                if(isset($type)){
                    if($type=='new'){
                        $query->orderBy('created_at','desc');
                    }
                    if($type=='user'){
                        $query->orderBy('user_count','desc');
                    }
                }else{
                    $query->orderBy('created_at','asc');
                }
            });
          if(isset($recommend)){
              return $this->model->get();
          }else{
              return parent::index($request); // TODO: Change the autogenerated stub
          }

    }

    public function  show($id, Request $request)
    {

        return parent::show($id, $request); // TODO: Change the autogenerated stub
    }

    public  function  recommendCourse(Request $request){
            return $this->model->query()->recomend()
                ->where(function($query )use($request) {
                     if($request->type=='index'){
                         $query->limit(4);
                     }
                 })
                ->get();
    }

    public function playVideo(Request $request,$id){

        $video=$this->model->where('id',$id)->first();
        if(!@$video){
            apiError('没有该教程资源');
        }
        event(new AttendCourse($request->id,getLoginUserID()));
        return $video;
    }

    public  function collectCourse(Request $request){
          $coures= $this->model->where('id',$request->course_id)->first();
          if(!@$coures){
            apiError('没有该教程资源');
          }
          $userID=getLoginUserID();

          $collect= UserCollect::query()->where('user_id',$userID)->where('course_id',$request->course_id)->first();

         if(($collect==null)){
            $data=[
                'user_id'=>$userID,
                'course_id'=>$request->course_id
            ];
            UserCollect::query()->insert($data);
            return Response()->success('收藏成功','收藏成功');
         }else{
             $collect->delete();
             return Response()->success('取消收藏','取消收藏');

         }
    }
}